Skip to content

Implement unstable trait impl #140399

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 34 commits into
base: master
Choose a base branch
from
Draft

Implement unstable trait impl #140399

wants to merge 34 commits into from

Conversation

tiif
Copy link
Member

@tiif tiif commented Apr 28, 2025

This PR allows marking impls of stable trait with stable type as unstable.

The design and mentoring are done by @BoxyUwU

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver) labels Apr 28, 2025
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@BoxyUwU BoxyUwU self-assigned this May 27, 2025
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Collaborator

bors commented Jun 10, 2025

☔ The latest upstream changes (presumably #142299) made this pull request unmergeable. Please resolve the merge conflicts.

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Collaborator

bors commented Jun 14, 2025

☔ The latest upstream changes (presumably #142483) made this pull request unmergeable. Please resolve the merge conflicts.

Copy link
Member

@BoxyUwU BoxyUwU left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gg gamer

Comment on lines 805 to 808
let unstable_feature_stab =
find_attr!(attrs, AttributeKind::AllowUnstableFeature(i) => i)
.map(|i| i.as_slice())
.unwrap_or_default();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this won't work if there are multiple unstable_feature_bound attributes. E.g.

#[unstable_feature_bound(feat_foo)]
#[unstable_feature_bound(feat_bar)]
#[unstable(feature = "feat_bar", issue = "none")]
impl Foo for Bar {}

you should be able to do something like:
find_attr!(attrs, AttributeKind::AllowUnstableFeature(i) if i == feature)
to get a yes/no as to whether there's an unstable feature bound on the impl with the same symbol as the stability attr

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This reminds me, I am not sure if I handled multiple attributes case correctly in compiler/rustc_hir_analysis/src/collect/predicates_of.rs. I will take a look at that too.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, yes, I don't think that's correct either

Copy link
Member Author

@tiif tiif Jun 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did a bit of tracing, and found this surprisingly works,

I tested with

#[unstable_feature_bound(feat_bar, feat_koo)]
#[unstable_feature_bound(feat_foo, feat_moo)]
#[unstable(feature = "feat_bar", issue = "none")]
impl Foo for Bar {
}

and it got every symbols on unstable_feature_bound

According to the trace, I suspect the two #[unstable_feature_bound] are parsed as one in attrs?

annotate(id = DefId(0:6 ~ unstable_feature_bound_attr_test[4947]::{impl#0}),
 attrs = [Parsed(Stability { 
 stability: Stability { level: Unstable { reason: None, issue: None, is_soft: false, implied_by: None, old_name: None }, feature: "feat_bar" }, 
 span: tests/ui/unstable-feature_bound/unstable_feature_bound_attr_test.rs:16:1: 16:50 (#0) }), 
 
 Parsed(UnstableFeatureBound([("feat_bar", 
 tests/ui/unstable-feature_bound/unstable_feature_bound_attr_test.rs:14:1: 14:46 (#0)), 
 ("feat_koo", tests/ui/unstable-feature_bound/unstable_feature_bound_attr_test.rs:14:1: 14:46 (#0)), 
 ("feat_foo", tests/ui/unstable-feature_bound/unstable_feature_bound_attr_test.rs:15:1: 15:46 (#0)), 
 ("feat_moo", tests/ui/unstable-feature_bound/unstable_feature_bound_attr_test.rs:15:1: 15:46 (#0))]))])

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a test for this tests/ui/unstable-feature_bound/unstable_feature_bound_multi_attr.rs

Comment on lines +840 to +842
fn enabled(self, symbol: <TyCtxt<'tcx> as Interner>::Symbol) -> bool {
self.enabled(symbol)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fn enabled(self, symbol: <TyCtxt<'tcx> as Interner>::Symbol) -> bool {
self.enabled(symbol)
}
fn feature_bound_holds_in_crate(self, symbol: <TyCtxt<'tcx> as Interner>::Symbol) -> bool {
// We don't consider feature bounds to hold in the crate if we `staged_api` feature is
// even when the feature is enabled. This is to prevent accidentally leaking unstable APIs
// to stable.
!self.staged_api() && self.enabled(symbol)
}

Copy link
Member Author

@tiif tiif Jun 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tweaked the comment by a little bit

        // We don't consider feature bounds to hold in the crate when `staged_api` feature is
        // enabled, even if it is enabled through `#[feature]`. 
        // This is to prevent accidentally leaking unstable APIs to stable.

hope this captured what you intended to say :D

//
// Note: we don't consider a feature to be enabled
// if we are in std/core even if there is a corresponding `feature` attribute on the crate.
if (!self.selcx.tcx().features().staged_api()
Copy link
Member Author

@tiif tiif Jun 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to use feature_bound_holds_in_crate in the old solver, just like what is done in the new solver, but it requires rustc_type_ir::inherent::*, and has scary message

error: do not use `rustc_type_ir::inherent` unless you're inside of the trait solver
  --> compiler/rustc_trait_selection/src/traits/fulfill.rs:15:20
   |
15 | use rustc_type_ir::inherent::*;
   |                    ^^^^^^^^
   |
   = note: the method or struct you're looking for is likely defined somewhere else downstream in the compiler
   = note: `-D rustc::usage-of-type-ir-inherent` implied by `-D warnings`
   = help: to override `-D warnings` add `#[allow(rustc::usage_of_type_ir_inherent)]`

so i decided to just write it out manually :)

Comment on lines 8 to 9
#[unstable_feature_bound(feat_bar)]
//~^ ERROR: stability attributes may not be used outside of the standard library
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to change the error message to unstable_feature_bound may not be used outside of the standard library or it is acceptable to call it stability attribute?

@tiif tiif changed the title WIP: Unstable impls Implement unstable trait impl Jun 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-attributes Area: Attributes (`#[…]`, `#![…]`) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants